/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package catalog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import users.UsersFromXMLHandler;

/**
 * This class stores a collection of CDs
 * Evetually it will implement filtering
 * @author smoniz
 *
 */
public class Catalog {

    static Catalog  cat = XMLParser.getCatalog();

   
    List<CD> cds = new ArrayList<CD>();

    public static Catalog getCat() {
        return cat;
    }

    public void addCD(CD cd) {
        cds.add(cd);
    }

    public CD getCD(String title) {
        for (CD cd : cds) {
            if (cd.getTitle().equals(title)) {
                return cd;
            }

        }
        return null;
    }

    public void printCDs() {
        for (CD cd : cds) {
            System.out.println(cd.toString());

        }
    }
    //Get Artists from Genres
    //Get CDs From Artists
    //Get Genres from ALL
    ArrayList<String> Genres = null;

    public List<String> getGenres() {
        if (Genres == null) {
            Genres = new ArrayList<String>();
            for (CD cd : cds) {
                if (!Genres.contains(cd.getGenre())) {
                    Genres.add(cd.getGenre());
                }
            }


        }
        return Genres;

    }

    public List<String> getArtists(Object[] genres) {

        List Genres = Arrays.asList(genres);
        ArrayList<String> Artists = new ArrayList<String>();
        for (CD cd : cds) {
            if (Genres.contains(cd.getGenre()) && !Artists.contains(cd.getArtist())) {
                Artists.add(cd.getArtist());

            }
        }
        if (Artists.size() <= 0) {
            return null;
        }
        return Artists;
    }

    public List<String> getCDs(Object[] artists) {
        List Artists = Arrays.asList(artists);
        ArrayList<String> Titles = new ArrayList<String>();
        for (CD cd : cds) {
            if (Artists.contains(cd.getArtist()) && !Titles.contains(cd.getTitle())) {
                Titles.add(cd.getTitle());

            }
        }
        if (Titles.size() <= 0) {
            return null;
        }
        return Titles;
    }

    public List<String> search(String genre, String artist, String title) {
        List<CD> g = searchgenre(genre);
        List<CD> a = searchartist(artist);

        List<CD> t = searchtitle(title);
        g.retainAll(a);
        g.retainAll(t);
        ArrayList<String> titles = new ArrayList<String>();
        for (CD cd : g) {
            titles.add(cd.getTitle());
        }
        return titles;



    }

    private List<CD> searchartist(String artist) {
        artist = artist.trim();
        ArrayList<CD> res = new ArrayList<CD>();
        if (artist.trim().equals("")) {
            return cds;



        } else {
            for (CD cd : cds) {
                if (cd.getArtist().toUpperCase().contains(artist.toUpperCase())) {
                    res.add(cd);

                }


            }
        }

        return res;

    }

    public List<CD> searchtitle(String title) {
        title = title.trim();
        ArrayList<CD> res = new ArrayList<CD>();
        if (title.trim().equals("")) {
            return cds;



        } else {
            for (CD cd : cds) {
                if (cd.getTitle().toUpperCase().contains(title.toUpperCase())) {
                    res.add(cd);

                }


            }
        }

        return res;

    }

    private List<CD> searchgenre(String genre) {
        genre = genre.trim();
        ArrayList<CD> res = new ArrayList<CD>();
        if (genre.trim().equals("")) {
            return cds;



        } else {
            for (CD cd : cds) {
                if (cd.getGenre().toUpperCase().contains(genre.toUpperCase())) {
                    res.add(cd);

                }


            }
        }

        return res;

    }
}
